/*
 * MusicalSystem.java
 *
 * This program demonstrate how to use tree and handles events.
 *
 * Copyright (c) 2007 Aptech Software Limited. All Rights Reserved.
 */
package demo.controls;

import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author Vincent
 */
public class MusicalSystem extends javax.swing.JFrame {

    /**
     * Creates new form MusicalSystem
     */
    public MusicalSystem() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }

        getContentPane().setBackground(new Color(212, 208, 200));
        initComponents();
        rootNode = new DefaultMutableTreeNode("Music");
        dtmChild = new DefaultTreeModel(rootNode);
        treOperatingSystem.setModel(dtmChild);

        for (int i = 0; i < music.length; i++) {
            dtmMusic[i] = new DefaultMutableTreeNode(music[i]);
            rootNode.add(dtmMusic[i]);
        }

        for (int i = 0; i < ClassicalType.length; ++i) {
            dtmMusic[0].add(new DefaultMutableTreeNode(ClassicalType[i]));
        }

        for (int i = 0; i < JazzType.length; ++i) {
            dtmMusic[1].add(new DefaultMutableTreeNode(JazzType[i]));
        }

        treOperatingSystem.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scpOperatingSystem = new javax.swing.JScrollPane();
        treOperatingSystem = new javax.swing.JTree();
        lblStatus = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Operating System");
        setResizable(false);
        getContentPane().setLayout(new java.awt.FlowLayout());

        scpOperatingSystem.setPreferredSize(new java.awt.Dimension(200, 300));

        treOperatingSystem.setPreferredSize(new java.awt.Dimension(180, 200));
        treOperatingSystem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                treOperatingSystemMouseClicked(evt);
            }
        });
        scpOperatingSystem.setViewportView(treOperatingSystem);

        getContentPane().add(scpOperatingSystem);

        lblStatus.setOpaque(true);
        lblStatus.setPreferredSize(new java.awt.Dimension(230, 20));
        getContentPane().add(lblStatus);

        setSize(new java.awt.Dimension(240, 363));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void treOperatingSystemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treOperatingSystemMouseClicked
// TODO add your handling code here:
        TreePath tp =
                treOperatingSystem.getPathForLocation(
                evt.getX(), evt.getY());

        if (tp != null) {
            String str = tp.toString();
            int index = str.lastIndexOf(",");
            if (index != -1) {
                str = str.substring(index + 1, str.length() - 1);
                lblStatus.setText(str);
            } else {

                str = str.substring(1, str.length() - 1);
                lblStatus.setText(str);
            }
        }
    }//GEN-LAST:event_treOperatingSystemMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MusicalSystem().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblStatus;
    private javax.swing.JScrollPane scpOperatingSystem;
    private javax.swing.JTree treOperatingSystem;
    // End of variables declaration//GEN-END:variables
    DefaultMutableTreeNode rootNode;
    DefaultMutableTreeNode dtmMusic[] = new DefaultMutableTreeNode[4];
    DefaultTreeModel dtmChild;
    String music[] = {"Classical", "Jazz", "Rock"};
    String ClassicalType[] = {"Bethoven", "Brahms", "Mozart"};
    String JazzType[] = {"Albert Ayler", "Chet Barker", "Johm Coltrone", "Miles Davis"};
}
